from typing import *


class NumMatrix:

    def __init__(self, matrix: List[List[int]]):
        m, n = len(matrix) + 1, len(matrix[0]) + 1
        self.sum = [[0] * n for _ in range(m)]
        for i in range(1, m):
            for j in range(1, n):
                self.sum[i][j] = self.sum[i][j - 1] + matrix[i - 1][j - 1]

        for i in range(1, m):
            for j in range(1, n):
                self.sum[i][j] += self.sum[i - 1][j]

    def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
        return self.sum[row2 + 1][col2 +
                                  1] + self.sum[row1][col1] - self.sum[row1][
                                      col2 + 1] - self.sum[row2 + 1][col1]


nm = NumMatrix([[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5],
                [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]])

# Your NumMatrix object will be instantiated and called as such:
# obj = NumMatrix(matrix)
# param_1 = obj.sumRegion(row1,col1,row2,col2)